package com.xr.core.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xr.core.system.common.annotation.OperationLogAnno;
import com.xr.core.system.common.base.MyPage;
import com.xr.core.system.common.base.Result;
import com.xr.core.system.common.enums.OperationType;
import com.xr.core.system.common.groups.Save;
import com.xr.core.system.common.groups.Update;
import com.xr.core.system.entity.permissionModule.Permission;
import com.xr.core.system.service.PermissionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 总后台 权限表(Permission)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/permission")
public class PermissionController {

    /**
     * 服务对象
     */
    @Resource
    private PermissionService permissionService;

    /**
     * 获取整个权限树
     *
     * @return
     */
    @GetMapping("/permissionTree")
    public Result<List<Permission>> getPermissionTree() {
        return Result.success(permissionService.getPermissionTree());
    }

    /**
     * 停启用权限
     *
     * @param id
     * @return
     */
    @DeleteMapping("/menuOrPermission")
    @OperationLogAnno(module = "权限表模块", type = OperationType.UPDATE, desc = "停启用权限")
    Result<Object> delMenuOrPermission(Long id) {
        Permission permission = new Permission();
        LambdaUpdateWrapper<Permission> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Permission::getInitial, false).setSql("enable = not enable").eq(Permission::getId, id);
        return Result.isSuccess(permissionService.update(permission, updateWrapper));
    }

    /**
     * 查询所有数据
     *
     * @param permission 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryAll")
    public Result<List<Permission>> queryAll(Permission permission) {
        permission.setEnable(true);
        return Result.success(permissionService.list(new QueryWrapper<>(permission)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param permission 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryByPage")
    public Result<MyPage<Permission>> queryByPage(MyPage<Permission> page, Permission permission) {
        return Result.success(permissionService.page(page, new QueryWrapper<>(permission)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOne/{id}")
    public Result<Permission> selectOne(@PathVariable Serializable id) {
        return Result.success(permissionService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param permission 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @OperationLogAnno(module = "权限模块", type = OperationType.INSERT, desc = "新增数据")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Permission permission) {
        return Result.isSuccess(permissionService.insert(permission));
    }

    /**
     * 修改数据
     *
     * @param permission 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @OperationLogAnno(module = "权限模块", type = OperationType.UPDATE, desc = "修改数据")
    public Result<Object> update(@Validated(Update.class) @RequestBody Permission permission) {
        return Result.isSuccess(permissionService.update(permission));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @OperationLogAnno(module = "权限模块", type = OperationType.DELETE, desc = "删除单条数据")
    public Result<Object> delete(@PathVariable Long id) {
        Permission permission = new Permission();
        LambdaUpdateWrapper<Permission> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Permission::getId, id).eq(Permission::getInitial, false);
        return Result.isSuccess(permissionService.deleteWithFill(permission, updateWrapper));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/batchDelete")
    @OperationLogAnno(module = "权限模块", type = OperationType.DELETE, desc = "批量删除数据")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        Permission permission = new Permission();
        permission.setInitial(false);
        return Result.isSuccess(permissionService.batchDelete(idList, permission));
    }

}

